/**
 * 
 */
package grid;

import java.util.ArrayList;
import java.util.List;
import static grid.Constants.*;

/**
 * @author Darko
 * 
 */
public class BreederNode
        extends Node {

    protected int island;
    protected int epoch;
    protected int seasons;
    protected int islands;
    protected int migrationWidth;
    protected static final String CLASS = "grid.Breeder";
    protected List<String> inputFiles;
    protected String outputFile;
    protected String logFile;

    public BreederNode(int island, int epoch, int seasons, int islands, int migrationWidth) {
        this.island = island;
        this.epoch = epoch;
        this.islands = islands;
        this.migrationWidth = migrationWidth;

        this.seasons = seasons;
        this.name = "breeder" + island + "_" + epoch;

        inputFiles = new ArrayList<String>();
        for (int i = -migrationWidth; i <= migrationWidth; i++) {
            int index = island + i <= 0 ? islands + island + i : island + i;
            index = index > islands ? index - islands : index;
            if (epoch > 0) {
                inputFiles.add("population" + index + "_" + epoch + EXTENSION);
            }
        }
        this.outputFile = "population" + island + "_" + (epoch + 1) + EXTENSION;
        this.logFile = "breeder" + island + "_" + epoch + LOG_POSTFIX;

    }

    public String getArguments() {
        return "\"" + ((argumentsPrefix != null) ? (argumentsPrefix + " ") : "") + CLASS + " " +
                seasons + " " + epoch + " " + island + " " + islands + " " + migrationWidth + "\"";
    }

    public String getInputSandbox() {
        String input = "";
        if (epoch == 1) {
            for (String inputFile : inputFiles) {
                input += "\"" + inputFile + "\", ";
            }
        } else {
            for (Node inputNode : inputNodes) {
                input += "root.nodes." + inputNode.getName() + ".description.OutputSandbox[0], ";
            }
        }
        input = input.substring(0, input.length() - 2);
        return "{" + input + ", root.InputSandbox}";
    }

    public String getOutputSandbox() {
        return "{\"" + outputFile + "\", \"" + logFile + "\", \"" +
                this.getStdOutput() + "\", \"" + getStdError() + "\"}";
    }
}
